/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionabsence.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

/**
 *
 * @author huynal
 */
public class QuestionBDD {

    public QuestionBDD() {
    }

    public static Question questionAleatoire() throws SQLException {
        Question question = new Question();
        String sqlCount = "select count(*) from question";
        Statement st = Connection.getBDD().createStatement();
        st.execute(sqlCount);
        Random numeroQuestion = new Random();
        String sql = "select intitule from question where question.id=?";
        PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        pst.setInt(1, (int)(Math.random()%(numeroQuestion.nextInt(st.getResultSet().getRow())))+1);
        question.init(pst.getResultSet());
        return question;
    }
}
